//
//  ClearingView.m
//  TheDragon
//
//  Created by appleLJ on 2017/6/23.
//  Copyright © 2017年 LJ. All rights reserved.
//

#import "ClearingView.h"

@interface ClearingView()

@property(nonatomic,strong) UIView *bgView; //白色背景view
@property(nonatomic,strong) UILabel *titleLabel;//标题label
@property(nonatomic,strong) UILabel *contentLabel;//内容label
@property(nonatomic,strong) UIButton *cancelButton;//取消按钮
@property(nonatomic,strong) UIButton *continueButton;//继续按钮
@property(nonatomic,strong) UIView *hLineView;//横线view
@property(nonatomic,strong) UIView *vLineView;//竖线view

@end

@implementation ClearingView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.alpha = 0;
        UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        backView.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:0.75];
        [self addSubview:backView];
        [backView addSubview:self.bgView];
        
        //布局
        [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(144 / WIDTH_5S_SCALE);
            make.width.mas_equalTo(216 / WIDTH_5S_SCALE);
            make.center.equalTo(backView);
        }];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.bgView.mas_top).offset(15 / WIDTH_5S_SCALE);
            make.centerX.equalTo(self.bgView);
            make.height.mas_equalTo(15 / WIDTH_5S_SCALE);
        }];
        [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.titleLabel.mas_top).offset(25 / WIDTH_5S_SCALE);
            make.left.equalTo(self.bgView.mas_left).offset(25 / WIDTH_5S_SCALE);
            make.right.equalTo(self.bgView.mas_right).offset(-25 / WIDTH_5S_SCALE);
            make.centerX.equalTo(self.bgView);
        }];
        [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(108 / WIDTH_5S_SCALE);
            make.height.mas_equalTo(33 / WIDTH_5S_SCALE);
            make.bottom.equalTo(self.bgView.mas_bottom);
            make.left.equalTo(self.bgView.mas_left);
        }];
        [self.continueButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(108 / WIDTH_5S_SCALE);
            make.height.mas_equalTo(33 / WIDTH_5S_SCALE);
            make.bottom.equalTo(self.bgView.mas_bottom);
            make.right.equalTo(self.bgView.mas_right);
        }];
        [self.hLineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.cancelButton);
            make.width.mas_equalTo(self.bgView);
            make.height.mas_equalTo(1);
            make.centerX.equalTo(self.bgView);
        }];
        [self.vLineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.hLineView);
            make.width.mas_equalTo(1);
            make.bottom.equalTo(self.bgView);
            make.centerX.equalTo(self.hLineView);
        }];
        
    }
    return self;
}

#pragma mark -- init

- (UIView *)bgView{
    if (!_bgView) {
        _bgView = [[UIView alloc] init];
        _bgView.backgroundColor = getColor(whiteColor);
        _bgView.layer.cornerRadius = 5;
        _bgView.layer.masksToBounds = YES;
        
        //添加控件
        [_bgView addSubview:self.titleLabel];
        [_bgView addSubview:self.contentLabel];
        [_bgView addSubview:self.cancelButton];
        [_bgView addSubview:self.continueButton];
        [_bgView addSubview:self.hLineView];
        [_bgView addSubview:self.vLineView];
    }
    return _bgView;
}

- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = DEF_FontSize_18;
        _titleLabel.textColor = getColor(mainColor);
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.text = @"清除缓存";
    }
    return _titleLabel;
}

- (UILabel *)contentLabel{
    if (!_contentLabel) {
        _contentLabel = [[UILabel alloc] init];
        _contentLabel.font = DEF_FontSize_13;
        _contentLabel.textColor = [UIColor blackColor];
        _contentLabel.textAlignment = NSTextAlignmentLeft;
        _contentLabel.numberOfLines = 0;
        _contentLabel.text = @"确定要清除缓存吗？清除后数据将不再恢复！";
    }
    return _contentLabel;
}

- (UIButton *)cancelButton{
    if (!_cancelButton) {
        _cancelButton = [[UIButton alloc] init];
        [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        _cancelButton.titleLabel.font = DEF_FontSize_14;
        [_cancelButton setTitleColor:getColor(mainColor) forState:UIControlStateNormal];
        [_cancelButton addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
        //_cancelButton.backgroundColor = [UIColor redColor];
    }
    return _cancelButton;
}

- (UIButton *)continueButton{
    if (!_continueButton) {
        _continueButton = [[UIButton alloc] init];
        [_continueButton setTitle:@"继续" forState:UIControlStateNormal];
        _continueButton.titleLabel.font = DEF_FontSize_14;
        [_continueButton setTitleColor:getColor(mainColor) forState:UIControlStateNormal];
        [_continueButton addTarget:self action:@selector(continueBtnClick) forControlEvents:UIControlEventTouchUpInside];
        //_continueButton.backgroundColor = [UIColor yellowColor];
    }
    return _continueButton;
}

- (UIView *)hLineView{
    if (!_hLineView) {
        _hLineView = [[UIView alloc] init];
        _hLineView.backgroundColor = getColor(mainColor);
    }
    return _hLineView;
}

- (UIView *)vLineView{
    if (!_vLineView) {
        _vLineView = [[UIView alloc] init];
        _vLineView.backgroundColor = getColor(mainColor);
    }
    return _vLineView;
}

#pragma mark -- click

- (void)continueBtnClick{
    NSLog(@"继续");
    if (self.clearHandle) {
        self.clearHandle();
        [self cancelBtnClick];
    }
}

#pragma mark -- show

//出现
-(void)show{
    [[UIApplication sharedApplication].delegate.window addSubview:self];
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 1;
    }];
}

//消失
- (void)cancelBtnClick{
    NSLog(@"取消");
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 0 ;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

@end
